<?php

// This is a PLUGIN TEMPLATE.

// Copy this file to a new name like abc_myplugin.php.  Edit the code, then
// run this file at the command line to produce a plugin for distribution:
// $ php abc_myplugin.php > abc_myplugin-0.1.txt

// Plugin name is optional.  If unset, it will be extracted from the current
// file name. Uncomment and edit this line to override:
$plugin['name'] = 'wyn_comment_sofa';

$plugin['version'] = '0.3';
$plugin['author'] = 'wayne';
$plugin['author_uri'] = 'http://www.gopherwood.info/';
$plugin['description'] = 'Comment Sofa for Textpattern. Generate a commentor cloud or a list of most commentors. Have fun!';

// Plugin types:
// 0 = regular plugin; loaded on the public web side only
// 1 = admin plugin; loaded on both the public and admin side
// 2 = library; loaded only when include_plugin() or require_plugin() is called
$plugin['type'] = 0; 


@include_once('../zem_tpl.php');

if (0) {
?>
# --- BEGIN PLUGIN HELP ---

<p><strong>Comment Sofa plugin for Textpattern</strong></p>

<p>It can generate a cloud or a list for commenters who commented the most. OR generate them for users who got the sofa (first comment to a post) most :D</p>

<p><strong>History</strong></p>

<ul>
  <li>0.3 : Add Restrict <span class="caps">SOFA</span> mode, which generator a cloud or a list for the users who got sofa (the first one who comment to a post) most.</li>
  <li>0.2 : Add keyword attribute to identify user by name or email and get skip value accepted a list with &#8216;;&#8217; delimited.</li>
  <li>0.1 : Initialize Release</li>
</ul>

<p><strong>Usage</strong></p>

<ol>
  <li><strong>wyn_comment_sofa_cloud</strong>
    <ul>
      <li>wraptag: wrap your cloud with a tag</li>
      <li>keyword: The keyword to identify user, available values are name and email, default is name</li>
      <li>skip: skip the user by indicated thing, default is nothing (lots of blogs just have one admin user to do everything :D) You can set values more than one, just use a &#8216;;&#8217; to separate.</li>
      <li>label: the label  </li>
      <li>labeltag: tag of the label</li>
      <li>limit: the limit of commenters, default is 10</li>
      <li>class: the class of wrap</li>
      <li>maxsize / minsize: the max / min font size of the most / least commenter&#8217;s link. (Percent, default is 200 and 50)</li>
      <li>break: the break between links, default is a space</li>
      <li>showcount: whether to show the count of comments by each commenter, default is 1</li>
      <li>restricted: whether to get <span class="caps">RESTRICTED</span> <span class="caps">SOFA</span> <span class="caps">MODE</span> (Generate cloud by rank of first commented to posts), default is 0</li>
    </ul>
  </li>
  <li><strong>wyn_comment_sofa_list</strong>
    <ul>
     <li>wraptag: wrap your list with a tag</li>
     <li>keyword: The keyword to identify user, available values are name and email and default is name</li>
     <li>skip: skip the user by indicated thing, default is nothing (lots of blogs just have one admin user to do everything :D)</li>
     <li>label: the label</li>
     <li>labeltag: tag of the label</li>
     <li>limit: the limit of commenters, default is 10</li>
     <li>class: the class of wrap</li>
     <li>break: the break between links, default is a space</li>
     <li>showcount: whether to show the count of comments by each commenter, default is 1</li>
     <li>restricted: whether to get <span class="caps">RESTRICTED</span> <span class="caps">SOFA</span> <span class="caps">MODE</span> (Generate list by rank of first commented to posts), default is 0</li>
    </ul>
  </li>
</ol>

# --- END PLUGIN HELP ---
<?php
}

# --- BEGIN PLUGIN CODE ---

/* Plugin Name: Textpattern Comment Sofa
   Plugin Version: 0.3
   Plugin Author: wayne
   Author Homepage: http://gopherwood.info
   Google Codes: http://code.google.com/p/wyn-txp-plugins/
 */

function wyn_comment_sofa_fix_web ($web) {
	$out = '#';
	if (isset ($web) && $web != '') {
		if (preg_match ('/http:\/\//', $web)) {
			$out = $web; 
	} else {
		$out = 'http://' . $web;
	}
	}

	return $out;
}

function wyn_comment_sofa ($keyword, $skip, $limit) {
	$out = array ();

	$ss = split (';', $skip);

	if ('name' != $keyword && 'email' != $keyword) {
		return;
	}

	$cond = $keyword . "<>'' and visible='1' ";
	foreach ($ss as $s) {
		$cond .= " and (" . $keyword . "<>'" . trim ($s) . "') ";
	}
	
	$sql = "SELECT name, web, COUNT( * ) as count
			FROM txp_discuss " . safe_pfx('txp_discuss') . "
			WHERE " . $cond . "  
			GROUP BY " . $keyword . "  
			ORDER BY count DESC 
			LIMIT 0 , " . $limit . ";";

	$rs = startRows ($sql);

	if ($rs) {
		while ($c = nextRow($rs)) {			
			$out[] = $c;
		}
	}

	return $out;
}

function wyn_comment_sofa_list ($atts, $nothing = "") {
	extract (lAtts (array (
	'wraptag' => 'ul',
	'break'  => 'li',
	'skip' => '',
	'label' => '',
	'labeltag' => '',
	'limit'    => 10,
	'showcount' => 1,
	'class'    => __FUNCTION__,
	'keyword'    => 'name',
	'restricted' => 0, 
	), $atts));

  $out = NULL;
  if ($restricted) {
  	$out = wyn_comment_sofa_restrict ($keyword, $skip, $limit);
  } else {
    $out = wyn_comment_sofa ($keyword, $skip, $limit);
	}
	if ($out) {
		$html = array ();
		foreach ($out as $sofa) {	      
			$web = wyn_comment_sofa_fix_web ($sofa['web']);
			$html[] = '<a href="' . $web . '">' . $sofa['name'] . ($showcount ? ' (' . $sofa['count'] . ')' : '') . '</a>';
		}

		return doLabel ($label, $labeltag) . doWrap ($html, $wraptag, $break, $class);
	}
}

function wyn_comment_sofa_cloud ($atts, $nothing = "") {
	extract (lAtts (array (
	'wraptag' => '',
	'skip' => '',
	'label' => '',
	'labeltag' => '',
	'limit'    => 10,
	'class'    => __FUNCTION__,
	'maxsize' => 200,
	'minsize' => 50,
	'break' => ' ',
	'showcount' => 1,
	'keyword'    => 'name',
	'restricted' => 0, 
	), $atts));
  
  $out = NULL;
  if ($restricted) {
  	$out = wyn_comment_sofa_restrict ($keyword, $skip, $limit);
  } else {
    $out = wyn_comment_sofa ($keyword, $skip, $limit);
	}
	
	if ($out) {
		return doLabel ($label, $labeltag) . doCloud ($out, $wraptag, $class, $maxsize, $minsize, $break, $showcount);
	}
}


function doCloud ($out, $wraptag, $class, $maxsize, $minsize, $break, $showcount) {
	$html = "";

	$first = array_shift ($out);
	$maxcount = (int) $first['count'];
	array_unshift ($out, $first);

	$end = end ($out);
	$mincount = (int) $end['count'];

	$countrange = $maxcount - $mincount;
	$sizerange = $maxsize - $minsize;

	$rank = $sizerange / $countrange;

	$size = count ($out);
	for ($i = 0; $i < $size; $i++) {
		$sofa_key = array_rand ($out);
		$sofa = $out[$sofa_key];
		$fontsize = ((int) $sofa['count'] - $mincount) * $rank + $minsize;
		$web = wyn_comment_sofa_fix_web ($sofa['web']);

		$html .= ($i ? $break : '') . '<a href="' . $web . '" style="font-size:' . $fontsize . '%;">' . $sofa['name'] . ($showcount ? '<sup>' . $sofa['count'] . '</sup>' : '') . '</a>';

		unset ($out[$sofa_key]);
	}

	return $wraptag != '' ? '<' . $wraptag . ' class="' . $class . '">' . $html . '</' . $wraptag . '>' : $html;
}

function wyn_comment_sofa_restrict ($keyword, $skip, $limit) {
	$out = array ();

	$ss = split (';', $skip);

	if ('name' != $keyword && 'email' != $keyword) {
		return;
	}


	$sql_id = "SELECT ID 
			FROM " . safe_pfx('textpattern') . " 
			WHERE comments_count<>'0';";

	$rs_id = startRows ($sql_id);
	if ($rs_id) {
		$cond = $keyword . "<>'' and visible='1' ";
		foreach ($ss as $s) {
			$cond .= " and (" . $keyword . "<>'" . trim ($s) . "') ";
		}
		
		$commentors = array ();
		$sofas = array ();
		
		while ($r = nextRow ($rs_id)) {
			$sql = "SELECT name, web, email 
				FROM " . safe_pfx ('txp_discuss') . " 
				WHERE " . $cond . " and (parentid='" . $r['ID'] . "')  
				ORDER BY posted
				limit 1;";

			$rs = startRows ($sql);
			if ($rs) {
				if ($c = nextRow($rs)) {
					$key = $c[$keyword];
					array_push ($sofas, $key);
					if (!isset ($commentors[$key])) {
						$commentors[$key] = $c;
					}
				}
			}
		}

		$sofas = array_count_values ($sofas);
		arsort ($sofas);

		$out = array ();
		$count = 0;
		$sofa_usrs = array_keys ($sofas);
		foreach ($sofa_usrs as $sofa_usr) {
			if ($count++ > $limit) {
				break;
			}

			$out[] = array (
					'name' => $commentors[$sofa_usr]['name'],
					'web' => $commentors[$sofa_usr]['web'],
					'email' => $commentors[$sofa_usr]['email'],
					'count' => $sofas[$sofa_usr]
				);
			}

		return $out;
	}
}

# --- END PLUGIN CODE ---

?>
